<?php

class Carloan {

    //public $gatewayUrl = "http://test.qiche100.com";
    public $gatewayUrl = "http://ysjr.qiche100.com";
    //public $gatewayUrl = "http://old.chedai.com";
    protected $appid = 'DYHL20170320';
    protected $appsecret = 'w5e2tomnb842u6dgh9fu';

    public function __construct() {
    }
    /**
     * 待支付列表
     * @param type $page
     * @param type $limit
     * @return type
     */
    public function getPayorder($dpcode = '' ,$page = 1,$limit = 10) {
        $params = array(
            'appid' => $this->appid,
            'page' => $page,
            'limit' => $limit,
            'dpcode' => $dpcode
        );
        $post_fields = $this->sign($params);
        $url = $this->gatewayUrl.'/Napi/pay_order/';
        $result = $this->curl($url, 'POST', $post_fields);
        return $result;
    }
    /**
     * 支付操作
     * @param type $pd_id
     * @return type
     */
    public function doPay($pd_id, $dpcode = ''){
        $params = array(
            'appid' => $this->appid,
            'id' => $pd_id
        );
        $post_fields = $this->sign($params);
        $sign = $this->sign(array('code'=>$dpcode));
        $url = $this->gatewayUrl.'/Napi/pay_load/?code='.$dpcode.'&sign='.$sign['sign'];
        $result = $this->curl($url, 'POST', $post_fields);
        return $result;
    }
    /**
     * 车辆信息同步
     * @param type $data
     * @return type
     */
    public function syncCar($data){
        $params = array(
            'appid' => $this->appid,
            'order_json' => json_encode($data)
        );
        $post_fields = $this->sign($params);
        $url = $this->gatewayUrl.'/Napi/sync_car/';
        $result = $this->curl($url, 'POST', $post_fields);
        return $result;
    }
    /**
     * 订单同步
     * @param type $data
     * @return type
     */
    public function syncOrder($data){
        $params = array(
            'appid' => $this->appid,
            'order_json' => json_encode($data)
        );
        $post_fields = $this->sign($params);
        $url = $this->gatewayUrl.'/Napi/sync_order/';
        $result = $this->curl($url, 'POST', $post_fields);
        return $result;
    }
    /**
     * 逾期记录
     * @param type $o_number
     * @param type $page
     * @param type $limit
     * @return type
     */
    public function overdue($o_number, $page = 1,$limit = 10){
        $params = array(
            'appid' => $this->appid,
            'page' => $page,
            'limit' => $limit,
            'o_number' => $o_number
        );
        $post_fields = $this->sign($params);
        $url = $this->gatewayUrl.'/Napi/over_due/';
        $result = $this->curl($url, 'POST', $post_fields);
        return $result;
    }
    /**
     * 催款记录
     * @param type $o_number
     * @param type $page
     * @param type $limit
     * @return type
     */
    public function dept($o_number, $page = 1,$limit = 10){
        $params = array(
            'appid' => $this->appid,
            'page' => $page,
            'limit' => $limit,
            'o_number' => $o_number
        );
        $post_fields = $this->sign($params);
        $url = $this->gatewayUrl.'/Napi/over_remark/';
        $result = $this->curl($url, 'POST', $post_fields);
        return $result;
    }
    /**
     * 贷后信息
     * @param type $o_number
     */
    function post_loan($o_number){
        $params = array(
            'appid' => $this->appid,
            'o_number' => $o_number
        );
        $post_fields = $this->sign($params);
        $url = $this->gatewayUrl.'/Napi/post_loan/';
        $result = $this->curl($url, 'POST', $post_fields);
        return $result;
    }
    
    function dictionary(){
        
    }
    /**
     * 身份证重复校验
     * @param type $cardno
     * @return type
     */
    function cardCheck($cardno){
        $params = array(
            'appid' => $this->appid,
            'card' => is_array($cardno) ? implode(',', $cardno) : $cardno
        );
        $post_fields = $this->sign($params);
        $url = $this->gatewayUrl.'/Napi/idcard_check/';
        $result = $this->curl($url, 'POST', $post_fields);
        return $result;
    }

    public function curl($url, $httpMethod = "GET", $postFields = null, $headers = null) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($this->readTimeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
        }
        if ($this->connectTimeout) {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
        }
        //https request
        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        if (is_array($headers) && 0 < count($headers)) {
            //curl_setopt($ch, CURLOPT_HEADER, true);
            $httpHeaders = self::getHttpHearders($headers);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
        }
        if ($httpMethod == "POST") {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        }
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            //throw new Exception(curl_error($ch), 0);
            return false;
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                //throw new Exception($response, $httpStatusCode);
                return false;
            }
        }
        curl_close($ch);
        return $response;
    }

    static function getHttpHearders($headers) {
        $httpHeader = array();
        foreach ($headers as $key => $value) {
            array_push($httpHeader, $key . ":" . $value);
        }
        return $httpHeader;
    }
    
    public function sign($params) {
        ksort($params);
        $str = http_build_query($params);
        $check_sign = md5($str.$this->appsecret);
        $params['sign'] = $check_sign;
        return $params;
    }
}
